package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.domain.dao.CoreVisitingRecordsMapper;
import com.corechina.apex_osswords.domain.dao.CoreVisitorMapper;
import com.corechina.apex_osswords.domain.dto.CoreVisitingRecordsDto;
import com.corechina.apex_osswords.domain.entity.CoreVisitingRecords;
import com.corechina.apex_osswords.domain.entity.CoreVisitor;
import com.corechina.apex_osswords.domain.vo.CoreVisitingRecordsVo;
import com.corechina.apex_osswords.service.ICoreVisitingRecordsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 来访日志 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-05-30
 */
@Service
public class CoreVisitingRecordsServiceImpl extends ServiceImpl<CoreVisitingRecordsMapper, CoreVisitingRecords> implements ICoreVisitingRecordsService {

    @Autowired
    private CoreVisitingRecordsMapper coreVisitingRecordsMapper;

    @Autowired
    private CoreVisitorMapper coreVisitorMapper;

    @Override
    public Response add(CoreVisitingRecordsDto coreVisitingRecordsDto) {
        CoreVisitor coreVisitor = coreVisitorMapper.selectById(coreVisitingRecordsDto.getVisitorId());
        if (coreVisitor == null) {
            return Response.error("未授权");
        }
        coreVisitor.setCardNo(coreVisitingRecordsDto.getCardNo());
        coreVisitor.setName(coreVisitingRecordsDto.getName());
        coreVisitor.setCompany(coreVisitingRecordsDto.getCompany());

        CoreVisitingRecords coreVisitingRecords = new CoreVisitingRecords();
        BeanUtils.copyProperties(coreVisitingRecordsDto, coreVisitingRecords);
        coreVisitingRecords.setVisitingTime(LocalDateTime.now());

        coreVisitingRecordsMapper.insert(coreVisitingRecords);
        coreVisitorMapper.updateById(coreVisitor);
        return Response.ok();
    }

    @Override
    public Response getList(Integer placeId, String company, String visitorName, String visitingTimeBegin, String visitingTimeEnd, String departureTimeBegin, String departureTimeEnd, Integer pageIndex, Integer pageSize) {
        IPage<CoreVisitingRecordsVo> visitingRecordsVoIPage = coreVisitingRecordsMapper.getList(new Page(pageIndex, pageSize), ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId(), placeId, company, visitorName, visitingTimeBegin, visitingTimeEnd, departureTimeBegin, departureTimeEnd);
        return Response.ok(visitingRecordsVoIPage.getRecords(), visitingRecordsVoIPage.getTotal());
    }

    @Override
    public Response getOne(Integer id) {
        return Response.ok(coreVisitingRecordsMapper.getOne(id));
    }

    public Response getVisitingStatistics(Integer placeId, String visitingTimeBegin, String visitingTimeEnd) {
        return Response.ok(coreVisitingRecordsMapper.getVisitingStatistics(ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId(), placeId, visitingTimeBegin, visitingTimeEnd));
    }
}
